package fr.umlv.m2.jee.pmlv.persistence.ldapLink;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

import fr.umlv.m2.jee.pmlv.persistence.subject.Subject;

/**
 * Represents the primary Key of the entity UserSubject.
 * @author jenseric
 *
 */
@Embeddable
@SuppressWarnings("serial")
public class UserSubjectPK implements Serializable {
	/**User id*/
	@Column(name="uid")
	private String uid;
	/**Subject*/
	@OneToOne
	@JoinColumn(name="subject_id")
	private Subject subject;
	/**Role of a User in a subject*/
	@Column(name="role")
        @Enumerated(EnumType.STRING)
	private RoleInSubject role;
	/**Year concerned by the relation between the user and the subject*/
	@Column(name="year")
	private long year;

	/**
	 * Sets the user id concerned by the relation.
	 * @param uid the user id to be set.
	 */
	public void setUid(String uid) {
		this.uid = uid;
	}

	/**
	 * Returns the user id concerned by the relation.
	 * @return the user id concerned by the relation.
	 */
	public String getUid() {
		return uid;
	}

	/**
	 * Sets the subject concerned by the relation.
	 * @param subject the subject concerned by the relation to be set.
	 */
	public void setSubject(Subject subject) {
		this.subject = subject;
	}
	
	/**
	 * Returns the subject concerned by the relation.
	 * @return the subject concerned by the relation.
	 */	
	public Subject getSubject() {
		return subject;
	}
	
	/**
	 * Sets the role concerned by the relation.
	 * @param role the role of the user to be set.
	 */
	public void setRole(RoleInSubject role) {
		this.role = role;
	}
	
	/**
	 * Returns the role of the user in this relation.
	 * @return the role of the user in this relation.
	 */
	public RoleInSubject getRole() {
		return role;
	}

	/**
	 * Sets the year concerned by the relation.
	 * @param year the year concerned by the relation to be set.
	 */
	public void setYear(long year) {
		this.year = year;
	}

	/**
	 * Returns the year concerned by the relation.
	 * @return the year concerned by the relation.
	 */
	public long getYear() {
		return year;
	}


	
	@Override
	public boolean equals(Object obj) {
		if(obj == this) {
			return true;
		}
		if(!(obj instanceof UserSubjectPK)) {
			return false;
		}
		UserSubjectPK u = (UserSubjectPK) obj;
		
		return uid.equals(u.uid) && (int)subject.getId() == (int)u.subject.getId()
				&& role.equals(u.role) && year == u.year; 
	}
	
	@Override
	public int hashCode() {
		return uid.hashCode() ^ (int)subject.getId() ^ (int) year;
	}




	
}
